//
//  LGSportTrainRecordCell.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/2/28.
//  训练记录Cell

#import "LGSportTrainRecordCell.h"
#import "LGSportTrainHeaderView.h"
#import "LGSportTrainItemCell.h"

#define itemW  viewPix(630)

@interface LGSportTrainRecordCell()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGSportTrainHeaderView *headView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
@end

@implementation LGSportTrainRecordCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}


-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.headView.dateStr = LGNSString(dataDic[@"date_str"]);
    self.headView.timeStr = LGNSString(dataDic[@"date_times"]);
    self.dataArray = dataDic[@"datalists"];
    [self.tableView reloadData];
    [self refreshHeight];
//    [self performSelector:@selector(refreshHeight) withObject:nil afterDelay:0.2];
}

-(void)refreshHeight{
    CGFloat height = self.tableView.contentSize.height;
    self.scrollView.contentSize = CGSizeMake(itemW, height);
    self.tableView.frame = CGRectMake(0, 0, itemW, height);
    [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportTrainItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if(self.delegate && [self.delegate respondsToSelector:@selector(trainRecord:)]){
        [self.delegate trainRecord:LGNSString(self.dataArray[indexPath.row][@"train_id"])];
    }
}

-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat height = self.scrollView.contentSize.height;
    if(height != self.tableView.contentSize.height){
        self.scrollView.contentSize = CGSizeMake(itemW, self.tableView.contentSize.height);
        self.tableView.frame = CGRectMake(0, 0, itemW, self.tableView.contentSize.height);
        [self.scrollView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(self.tableView.contentSize.height));
            [self.superTableView beginUpdates];
            [self.superTableView endUpdates];
        }];
    }
}

-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self.contentView addSubview:self.scrollView];
    [self.scrollView addSubview:self.tableView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(8));
        make.bottom.equalTo(self.contentView).offset(-viewPix(8));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.height.equalTo(@(self.tableView.contentSize.height));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
    }];
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] init];
        [_scrollView setShadoWithColor:@"#B0B2B2" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.contentSize = CGSizeMake(itemW, viewPix(90));
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, itemW, viewPix(90)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGSportTrainItemCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = self.headView;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, itemW, 0.01)];
    }
    return _tableView;
}

-(LGSportTrainHeaderView *)headView{
    if(!_headView){
        _headView = [[LGSportTrainHeaderView alloc] initWithFrame:CGRectMake(0, 0, itemW, viewPix(90))];
    }
    return _headView;
}

@end
